import { get_query_string } from "@/plugins/util";
import { realdataStore } from '../stores/realdata';

import pinia from "@/stores/pinia";
const realdata_store = realdataStore(pinia)// 这里一定要把 pinia传入进去;

const {ch_data_12_arr} = realdata_store;



// const real_data_store = realdataStore();
// const { ch_sys_type } = real_data_store;

/*电机试验台返回的时间戳最终格式化是 2022-06-20 17:07:35 */
let local_time = 0;


let data01_time = "20210620165903"; //设备的实时时间 socket
let data02_time = "20210620165903";
let data03_time = "20210620165903";
let data04_time = "20210620165903";


let time_split = 1000 * 60; //设备掉线标识。时间间隔。

function handle_socket_massage(res) {
    var res_data = null;

    if (get_query_string(console) == 1) {
        console.log("接收到数据", res, typeof res);
    }

    local_time = new Date().getTime(); //当前时间毫秒数

    try {

        if(typeof res.data=='string'){
            res_data = JSON.parse(res.data);
        }else{
            res_data = res.data;
        }

        if (get_query_string("console") == 1) {
            console.log("res.data[0]-----****------", res_data[0]);
        }

        let cc = {
            deviceId: 1,
            timestamp: 202006011343,
            data: {
                UAB: 1.1,
                UBC: 2,
            },
        };

        //处理数据，同步数据到pinia;

        /*开始处理 功率计*/
        if (res_data[0].deviceId == 1) {
            //     var device_temp_status = [];
            //   res_data.forEach((pcs_item, key) => {
            //     device_temp_status.push(
            //       juge_device_network(local_time, pcs_item.timestamp)
            //     );
            //   });
            // store.commit("set_data01", res_data[0].data);
        } else if (res_data[0].deviceId == 2) {


        } else if (res_data[0].deviceId == 3) {

        } else if (res_data[0].deviceId == 4) {

        }else if(res_data[0].deviceId==12){
            //[{"deviceId":12,"data":{"PA":1,"time":1}}]

            ch_data_12_arr(res_data[0].data.PA,res_data[0].data.time)
        }

        console.log("realdata_store 被加载",realdata_store)
    } catch (err) {
        if (res.data.indexOf("cmd") > -1) {
            console.log("服务端反馈");
            console.log(res.data);
        } else {
            console.log("解析JSON数据失败——error", err);
            console.log("失败的数据res_data", res_data);
            console.log("失败的数据typeof res_data", typeof res_data);
            console.log("失败的数据res.data", res.data);
            console.log("失败的数据typeof res.data", typeof res.data);
        }
    }
}

function juge_device_network(timestamp1, timestamp2) {
    if (get_query_string("console") == 1) {
        // console.log("本地时间和socket时间分别是", timestamp1, timestamp2);
    }

    let new_timestamp2 = new Date(
        formatter_socket_time(timestamp2 + "")
    ).getTime();

    if (get_query_string("console") == 1) {
        // console.log("本地时间和socket时间分别是格式化后", timestamp1, timestamp2);
        console.log(timestamp1, "---formatter_socket_time---", new_timestamp2);
    }

    if (Math.abs(timestamp1 - new_timestamp2) > time_split) {
        return false;
    } else {
        return true;
    }
}

function formatter_socket_time(time_str) {
    return (
        time_str.slice(0, 4) +
        "-" +
        time_str.slice(4, 6) +
        "-" +
        time_str.slice(6, 8) +
        " " +
        time_str.slice(8, 10) +
        ":" +
        time_str.slice(10, 12) +
        ":" +
        time_str.slice(12, 14)
    );
}

function interval_judge_device() {
    setInterval(() => {
        let local_time = new Date().getTime();

        if (juge_device_network(local_time, data01_time)) {
            //断开是false;
            console.log("设备1未断开--");
            // store.state.gray_action01 = false;
        } else {
            console.log("设备1进入--断开--");
            // store.state.gray_action01 = true;
        }

        if (juge_device_network(local_time, data02_time)) {
            // store.state.gray_action02 = false;
            console.log("设备2未断开--");
        } else {
            // store.state.gray_action02 = true;
            console.log("设备2----断开--");
        }

        if (juge_device_network(local_time, data03_time)) {
            // store.state.gray_action03 = false;
        } else {
            // store.state.gray_action03 = true;
        }

        if (juge_device_network(local_time, data04_time)) {
            console.log("设备4-未断开-", local_time, data04_time);
            // store.state.gray_action04 = false;
        } else {
            // store.state.gray_action04 = true;
            console.log("设备4-断开-", local_time, data04_time);
        }
    }, 10 * 1000);
}

// setTimeout(() => {
//     interval_judge_device();
// }, 5000)

export { handle_socket_massage };
